package com.jiaquyun.jcyx.main.mine

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.jetpack.mode.MineViewModel
import com.module.lemlin.owner.OwnerViewModeActivity
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.android.synthetic.main.activity_mine_share.*
import java.io.BufferedInputStream
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.net.URLConnection


class MineShareActivity : OwnerViewModeActivity<MineViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(MineShareActivity::class.java)
        }
    }

    override fun viewModel(): MineViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_share

    override fun stateBar(): StatusBar {
        return StatusBar(isLightMode = true, color = R.color.white, view = flMineShareBar)
    }

    override fun initView() {

        ivAppBack.setOnClickListener { finish() }
        tvMineShareSubmit.setOnClickListener {

            Thread{
                shareMini()
            }.start()

//            shareMini()
        }
    }

    private fun shareMini() {

        // 通过appId得到IWXAPI这个对象
        val wxapi = WXAPIFactory.createWXAPI(this, "wx504428242c9715b6")
        // 检查手机或者模拟器是否安装了微信
        if (!wxapi.isWXAppInstalled) {
            runOnUiThread {
                toast(message = "手机未安装微信")
            }
            return
        }
        val miniProgramObj = WXMiniProgramObject()
        miniProgramObj.webpageUrl = "http://www.qq.com" // 兼容低版本的网页链接

        miniProgramObj.miniprogramType =
            WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE // 正式版:0，测试版:1，体验版:2

        miniProgramObj.userName = "gh_4f6a09620eaf" // 小程序原始id

//        miniProgramObj.path = "/pages/my-data/inviteOrder?order_id=" //小程序页面路径；对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"
        miniProgramObj.path = "/pages/login/invitelogin?user_id=" + JCApplication.userInfo?.id //小程序页面路径；对于小游戏，可以只传入 query 部分，来实现传参效果，如：传入 "?foo=bar"

        val msg = WXMediaMessage(miniProgramObj)
        msg.title = "您的好友邀请您使用金城优选" // 小程序消息title

        msg.description = "" // 小程序消息desc
        val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_mine_share)
        msg.setThumbImage(bitmap) // 小程序消息封面图片，小于128k

        val req: SendMessageToWX.Req = SendMessageToWX.Req()
        req.transaction = System.currentTimeMillis().toString()
        req.message = msg
        req.scene = SendMessageToWX.Req.WXSceneSession // 目前只支持会话

        wxapi.sendReq(req)
    }


    fun getBitmap(url: String): Bitmap? {
        var bm: Bitmap? = null
        try {
            val iconUrl = URL(url)
            val conn: URLConnection = iconUrl.openConnection()
            val http: HttpURLConnection = conn as HttpURLConnection
            val length: Int = http.contentLength
            conn.connect()
            // 获得图像的字符流
            val `is`: InputStream = conn.getInputStream()
            val bis = BufferedInputStream(`is`, length)
            bm = BitmapFactory.decodeStream(bis)
            bis.close()
            `is`.close() // 关闭流
        } catch (e: Exception) {
            return null
        }
        return bm
    }
}